package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.repository;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowIssueNoticeListVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowIssueNoticeQueryVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowIssueNoticeUpdateStatusVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowIssueNoticeVo;
import com.iwhalecloud.citybrain.flow.platform.manager.common.ResponseResult;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.User;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowIssueNoticeRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.UserRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowIssueNotice;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowIssueNoticeExample;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.mapper.FlowIssueNoticeDAO;
import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.flowable.engine.common.impl.cfg.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lilh
 * @date 2019-04-25 14:34
 */
@Service
public class FlowIssueNoticeRepositoryImpl implements FlowIssueNoticeRepository {

    @Autowired
    private IdGenerator idGenerator;

    @Autowired
    private FlowIssueNoticeDAO flowIssueNoticeDAO;

    @Autowired
    private UserRepository userRepository;

    @Override
    public int insertIssueNotice(FlowIssueNoticeVo entity) {
        entity.setId(idGenerator.getNextId());
        return flowIssueNoticeDAO.insertSelective(convert(entity));
    }

    @Override
    public ResponseResult listIssueNotice(FlowIssueNoticeQueryVo flowIssueNoticeQueryVo) {
        User current = userRepository.getCurrentUser();
        FlowIssueNoticeExample query = new FlowIssueNoticeExample();
        FlowIssueNoticeExample.Criteria criteria = query.createCriteria().andReceiverCodeEqualTo(current.getUserCode());
        if (StringUtils.isNotEmpty(flowIssueNoticeQueryVo.getProcessName())) {
            criteria.andProcessNameLike("%" + flowIssueNoticeQueryVo.getProcessName() + "%");
        }
        if (StringUtils.isNotEmpty(flowIssueNoticeQueryVo.getMsgType())) {
            criteria.andMsgTypeEqualTo(flowIssueNoticeQueryVo.getMsgType());
        }
        if (StringUtils.isNotEmpty(flowIssueNoticeQueryVo.getSenderName())) {
            criteria.andSenderNameLike("%" + flowIssueNoticeQueryVo.getSenderName() + "%");
        }
        if (flowIssueNoticeQueryVo.getReadStatus() != null) {
            criteria.andReadStatusEqualTo(flowIssueNoticeQueryVo.getReadStatus());
        }

        query.setOrderByClause("read_status asc ,create_time desc");
        PageHelper.startPage(flowIssueNoticeQueryVo.getPageNum(), flowIssueNoticeQueryVo.getPageSize(), true);
        List<FlowIssueNotice> flowIssueNotices = flowIssueNoticeDAO.selectByExampleWithBLOBs(query);
        PageInfo<FlowIssueNoticeVo> pageInfo = new PageInfo<>(flowIssueNotices.stream().map(this::convert).collect(Collectors.toList()));
        List<FlowIssueNoticeListVo> result = pageInfo.getList().stream().map(FlowIssueNoticeListVo::new).collect(Collectors.toList());
        return ResponseResult.success().setTotalCount((int) pageInfo.getTotal()).setPageNum(pageInfo.getPageNum())
                .setPageSize(pageInfo.getPageSize()).setDatas(result);
    }


    @Override
    public int updateReadStatus(FlowIssueNoticeUpdateStatusVo flowIssueNoticeUpdateStatusVo) {
        FlowIssueNoticeExample query = new FlowIssueNoticeExample();
        FlowIssueNoticeExample.Criteria criteria = query.createCriteria().andReceiverCodeEqualTo(userRepository.getCurrentUser().getUserCode()).andReadStatusEqualTo(0);
        if (CollectionUtils.isNotEmpty(flowIssueNoticeUpdateStatusVo.getIds())) {
            //更新指定的记录
            criteria.andIdIn(flowIssueNoticeUpdateStatusVo.getIds());
        }
        FlowIssueNotice entity = new FlowIssueNotice();
        entity.setReadStatus(1);
        entity.setUpdateTime(new Date());

        return flowIssueNoticeDAO.updateByExampleSelective(entity, query);
    }

    @Override
    public FlowIssueNoticeVo detail(String id) {
        FlowIssueNoticeExample query = new FlowIssueNoticeExample();
        query.createCriteria().andIdEqualTo(id).andReceiverCodeEqualTo(userRepository.getCurrentUser().getUserCode());
        List<FlowIssueNotice> flowIssueNotices = flowIssueNoticeDAO.selectByExampleWithBLOBs(query);
        if (CollectionUtils.isNotEmpty(flowIssueNotices)) {
            return convert(flowIssueNotices.get(0));
        }
        return null;
    }

    @Override
    public int countIssueNotice() {
        User current = userRepository.getCurrentUser();
        FlowIssueNoticeExample query = new FlowIssueNoticeExample();
        query.createCriteria().andReadStatusEqualTo(0).andReceiverCodeEqualTo(current.getUserCode());
        return (int) flowIssueNoticeDAO.countByExample(query);
    }

    private FlowIssueNotice convert(FlowIssueNoticeVo entity) {
        if (Objects.isNull(entity)) {
            return null;
        }

        FlowIssueNotice result = new FlowIssueNotice();
        BeanUtils.copyProperties(entity, result);
        return result;
    }

    private FlowIssueNoticeVo convert(FlowIssueNotice entity) {
        if (Objects.isNull(entity)) {
            return null;
        }

        FlowIssueNoticeVo result = new FlowIssueNoticeVo();
        BeanUtils.copyProperties(entity, result);
        return result;
    }
}
